using System.IO;
using System.Reflection;
using GrapeCity.Documents.Excel;
using System;
using System.Data;

namespace GrapeCity.Documents.Excel.Examples.Templates.TemplateSamples.SalesTracker
{
    class Program
    {
        static void Main(string[] args)
        {
			//create a new workbook
			var workbook = new GrapeCity.Documents.Excel.Workbook();
			
			//Load template file from resource
			var templateFile = GetResourceStream("xlsx\\Template_SalesTracker.xlsx");
			workbook.Open(templateFile);
			
			#region Init Data
			var datasource = new DataTable();
			datasource.Columns.Add(new DataColumn("ProductName", typeof(string)));
			datasource.Columns.Add(new DataColumn("CostPerItem", typeof(double)));
			datasource.Columns.Add(new DataColumn("PercentMarkup", typeof(double)));
			datasource.Columns.Add(new DataColumn("TotalSold", typeof(Int32)));
			datasource.Columns.Add(new DataColumn("ShippingCharge", typeof(Int32)));
			datasource.Columns.Add(new DataColumn("ShippingCost", typeof(double)));
			datasource.Columns.Add(new DataColumn("Returns", typeof(Int32)));
			
			datasource.Rows.Add("Beverages", 10, 1, 15, 10, 5.75, 2);
			datasource.Rows.Add("Condiments", 11.5, 0.75, 18, 10, 5.75, 1);
			datasource.Rows.Add("Dairy Products", 13, 0.65, 20, 10, 6.25, 0);
			datasource.Rows.Add("Confections", 5, 0.9, 50, 5, 3.5, 0);
			datasource.Rows.Add("Sea Food", 4, 0.9, 42, 5, 3.25, 3);
			#endregion
			
			//Init template global settings
			workbook.Names.Add("TemplateOptions.KeepLineSize", "true");
			
			//Add data source
			workbook.AddDataSource("ds", datasource);
			//Invoke to process the template
			workbook.ProcessTemplate();
			        
			// Save to an excel file
			workbook.Save("SalesTracker.xlsx");

        }

		static Stream GetResourceStream(string resourcePath)
        {
            string resource = "SalesTracker.Resource." + resourcePath.Replace("\\", ".");
            var assembly = typeof(Program).GetTypeInfo().Assembly;
            return assembly.GetManifestResourceStream(resource);
        }

    }
}